package top.glike.ssl.auto.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.glike.ssl.auto.domain.SslDomainCertificate;
import top.glike.ssl.auto.service.IAutoSslConfService;
import top.glike.ssl.auto.service.ISslDomainCertificateService;
import top.glike.ssl.common.annotation.Log;
import top.glike.ssl.common.core.controller.BaseController;
import top.glike.ssl.common.core.domain.AjaxResult;
import top.glike.ssl.common.core.page.TableDataInfo;
import top.glike.ssl.common.enums.BusinessType;
import top.glike.ssl.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 域名管理Controller
 *
 * @author Sophia
 * @date 2025-10-12
 */
@RestController
@RequestMapping("/auto/certificate")
public class SslDomainCertificateController extends BaseController {

    @Autowired
    private ISslDomainCertificateService sslDomainCertificateService;
    @Autowired
    private IAutoSslConfService autoSslConfService;

    /**
     * 查询域名管理列表
     */
    @PreAuthorize("@ss.hasPermi('auto:certificate:list')")
    @GetMapping("/list")
    public TableDataInfo tlist(SslDomainCertificate sslDomainCertificate) {
        startPage();
        List<SslDomainCertificate> list = sslDomainCertificateService.selectSslDomainCertificateList(sslDomainCertificate);
        return getDataTable(list);
    }

    /**
     * 导出域名管理列表
     */
    @PreAuthorize("@ss.hasPermi('auto:certificate:export')")
    @Log(title = "域名管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SslDomainCertificate sslDomainCertificate) {
        List<SslDomainCertificate> list = sslDomainCertificateService.selectSslDomainCertificateList(sslDomainCertificate);
        ExcelUtil<SslDomainCertificate> util = new ExcelUtil<SslDomainCertificate>(SslDomainCertificate.class);
        util.exportExcel(response, list, "域名管理数据");
    }

    /**
     * 获取域名管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('auto:certificate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sslDomainCertificateService.selectSslDomainCertificateById(id));
    }

    /**
     * 新增域名管理
     */
    @PreAuthorize("@ss.hasPermi('auto:certificate:add')")
    @Log(title = "域名管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SslDomainCertificate sslDomainCertificate) {
        return toAjax(sslDomainCertificateService.insertSslDomainCertificate(sslDomainCertificate));
    }

    /**
     * 修改域名管理
     */
    @PreAuthorize("@ss.hasPermi('auto:certificate:edit')")
    @Log(title = "域名管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SslDomainCertificate sslDomainCertificate) {
        return toAjax(sslDomainCertificateService.updateSslDomainCertificate(sslDomainCertificate));
    }

    /**
     * 删除域名管理
     */
    @PreAuthorize("@ss.hasPermi('auto:certificate:remove')")
    @Log(title = "域名管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sslDomainCertificateService.deleteSslDomainCertificateByIds(ids));
    }

    /**
     * 推送证书到服务器
     */
    @PreAuthorize("@ss.hasPermi('auto:certificate:edit')")
    @Log(title = "推送证书到服务器", businessType = BusinessType.UPDATE)
    @DeleteMapping("/pushCertToServer/{id}")
    public AjaxResult pushCertToServer(@PathVariable Long id) {
        return toAjax(autoSslConfService.pushCertToServer(id));
    }

}
